import java.util.*;

public class StudentManage {
    private HashMap<String,Student> students;
    private Scanner input;
    public StudentManage(){
        init();
    }
    public StudentManage(HashMap<String, Student> stuMap, Scanner input) {
        this.students = stuMap;
        this.input = input;
    }
    public void init(){//初始化
        students = new HashMap<>();
        input = new Scanner(System.in);
        add((new Student("01","张三")));
        add((new Student("02","李四")));
        add((new Student("03","王五")));
        add((new Student("04","赵六")));
        showAll();
    }
    public void menu(){
        System.out.println("=========学生管理系统=========");
        System.out.println("1.添加学生");
        System.out.println("2.删除学生");
        System.out.println("3.修改学生信息");
        System.out.println("4.查看学生信息");
        System.out.println("5.显示所有信息");
        System.out.println("6.退出系统");
        System.out.println("============================");
        System.out.print("请选择1~6:");
    }
    public void run(){
        while(true){
            //显示所有学生信息
            /*showAll();*/
            //菜单界面
            menu();
            //从键盘接收一个数字
            int n = input.nextInt();
            switch (n){
                case 1 :
                    addAccount();
                    break;
                case 2:
                    deleteAccount();
                    break;
                case 3:
                    changeAccount();
                    break;
                case 4:
                    queryAccount();
                    break;
                case 5:
                    showAll();
                    break;
                case 6:
                    System.out.println("系统已退出");
                    System.exit(0);
                default:
                    break;
            }
        }
    }
    public void newAccount(Student student){
        String string = "";
        boolean ok = false;
        do{
            System.out.println("学号：");
            string = input.next();
            System.out.println("输入的学号是："+string);
            if(string.matches("[\\d]{2}")){
                student.setStuNo(string);
                ok = true;
            }/*else {
                System.out.println("error:学号必须是两位数字\n请重新输入");
                continue;
            }*/
            System.out.print("\t姓名：");
            string = input.next();
            student.setStuName(string);
            ok = true;
        }while(!ok);
    }
    public void addAccount(){
        System.out.println("请输入要添加学生的信息：");
        Student student1 = new Student();
        newAccount(student1);
        if(add(student1)){
            System.out.println("添加成功！");
            System.out.println(student1);
        }else{
            System.out.println("添加失败！");
        }
    }
    public void deleteAccount(){
        System.out.print("请输入要删除的学号：");
        String stuNo =input.next();
        Student student = remove(stuNo);
        if(student==null){
            System.out.println("不存在该联系人！");
        }else{
            System.out.println(student);
            System.out.println("学生删除成功！");
        }
        showAll();
    }
    public void changeAccount(){
        System.out.print("请输入要修改的学号：");
        String str = input.next();
        Student student = query(str);
        if(student==null){
            System.out.println("不存在该学生！");
            return ;
        }
        System.out.print("请输入要修改的学生姓名：");
        String stuName = input.next();
        student.setStuName((stuName));
        update(str,student);
        System.out.println("修改成功！");
        System.out.println(student);
    }
    public void queryAccount(){
        System.out.println("请输入要查询学生的学号：");
        String str =input.next();
        Student stu = query(str);
        if(stu == null){
            System.out.println("不存在该学生");
        }else{
            System.out.println(stu);
        }
    }
    public void showAll(){
        List<Student> list =getAll();
        Collections.sort((List<Student>)list);
        System.out.println("============================");
        System.out.println("总共"+list.size()+"条信息");
        for(Student student:list){
            System.out.println(student);
        }
        System.out.println();
        /*System.out.println("学生信息列表：");
        System.out.println("============================");
        System.out.println("总共"+students.size()+"条信息");
        Set<String> keySet =students.keySet();
        for (String key:keySet) {
            Student name = students.get(key);
            System.out.println("学生[学号"+key+",姓名："+name+"]");
        }*/
    }
    private boolean add(Student student){
        String stuNo = student.getStuNo();
        if(students.containsKey(stuNo)){
            return false;
        }else {
            students.put(stuNo,student);
            return true;
        }

    }
    private Student remove(String stuNo){
        return students.remove(stuNo);
    }
    private void update(String stuNo,Student student){
        students.remove(stuNo);
        students.put(student.getStuNo(),student);
    }
    private Student query(String stuNo){
        if(students.containsKey(stuNo)){
            return students.get(stuNo);
        }else{
            return null;
        }
    }
    private List<Student> getAll(){
        List<Student> lists = new ArrayList<>();
        Set<String> sets = students.keySet();
        for(String str:sets){
            lists.add(students.get(str));
        }
        return lists;
    }
}
